package com.kitty.game.mijing;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.activity.model.product.ActivityType;
import com.kitty.game.config.NPC;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.mijing.message.*;
import com.kitty.game.mijing.model.MiJingReward;
import com.kitty.game.mijing.model.MijingRank;
import com.kitty.game.role.model.Role;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.mina.core.session.IoSession;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

@Service
public class MinjingController {
   @Autowired
   private Dao dao;
   @Autowired
   JdbcTemplate jdbcTemplate;

   public void entermijing(Role role, NPC bossNpc) {
      String[] nameArr = new String[]{"离火", "乾金", "艮木", "坎水", "坤土"};
      int[] iconArr = new int[]{6284, 6283, 6271, 6281, 6286};
      Random random = new Random();
      String nameStr = nameArr[random.nextInt(nameArr.length)];
      int iconStr = iconArr[random.nextInt(iconArr.length)];
      int shengyucishu = SpringUtils.getActivityService().getRemainCount(role, ActivityType.mijing);
      Mijing_info mijing_info = new Mijing_info();
      mijing_info.setDlgflag((byte)1);
      mijing_info.setGuanqianum((short)role.getMijingcount());
      mijing_info.setName(nameStr);
      mijing_info.setWaiguan(iconStr);
      mijing_info.setQimenshengyu((short)shengyucishu);
      mijing_info.setGiftlenght(4);
      MessagePusher.pushMessage((Role)role, mijing_info);
   }

   @RequestMapping
   public void mijingrank(IoSession session, Mijing_rank mijing_rank) {
      Role role = SessionUtils.getRoleBySession(session);
      if (role != null) {
         if (mijing_rank.getStr().equals("rank_dlg")) {
            String sql = "select * from t_mijing order by guanqia desc, ";
            if (mijing_rank.getFlag() == 1) {
               sql = sql + " wushang desc ";
            } else if (mijing_rank.getFlag() == 2) {
               sql = sql + " fashang desc ";
            } else if (mijing_rank.getFlag() == 3) {
               sql = sql + " fangyu desc ";
            } else if (mijing_rank.getFlag() == 4) {
               sql = sql + " sudu desc ";
            }

            sql = sql + " limit 100";
            List<MijingRank> mijingRankList = this.jdbcTemplate.query(sql, new MijingRaw());
            System.out.println(mijingRankList.size());
            List<MijingMember> list1 = new ArrayList();
            int paihang = 0;

            for(int i = 0; i < mijingRankList.size(); ++i) {
               MijingRank mijingRank = (MijingRank)mijingRankList.get(i);
               if (mijingRank.getId() == role.getRoleId()) {
                  paihang = i + 1;
               }

               MijingMember mijingMember = new MijingMember();
               mijingMember.setGid(mijingRank.getGid());
               mijingMember.setGuankacount((short)mijingRank.getGuanqia());
               mijingMember.setParty(mijingRank.getParty());
               mijingMember.setName(mijingRank.getRolename());
               list1.add(mijingMember);
            }

            Mijing_paixue mijing_paixue = new Mijing_paixue();
            mijing_paixue.setFlag(mijing_rank.getFlag());
            mijing_paixue.setPaiming((short)paihang);
            mijing_paixue.setUsername(role.getName());
            mijing_paixue.setDangqianguanka((short)role.getMijingcount());
            mijing_paixue.setBangpai(role.getPartyName());
            mijing_paixue.setTime((int)(System.currentTimeMillis() / 1000L));
            mijing_paixue.setMijingMembers(list1);
            MessagePusher.pushMessage((IoSession)session, mijing_paixue);
         } else if (mijing_rank.getStr().equals("open_store")) {
            SpringUtils.getRoleService().sendupdatemijingjifen(role);
            RespQimenShop respQimenShop = new RespQimenShop();
            List<MijingShop> list = new ArrayList();
            List<MiJingReward> fetchList = this.dao.query(MiJingReward.class, Cnd.NEW());
            Iterator var15 = fetchList.iterator();

            while(var15.hasNext()) {
               MiJingReward miJingReward = (MiJingReward)var15.next();
               MijingShop mijingShop = new MijingShop();
               mijingShop.setWupinname(miJingReward.getName());
               mijingShop.setPrice(miJingReward.getPrice());
               DataCache.mijing_GOODS.put(miJingReward.getName(), miJingReward);
               list.add(mijingShop);
            }

            respQimenShop.setType((byte)7);
            respQimenShop.setList(list);
            MessagePusher.pushMessage((Role)role, respQimenShop);
         } else if (mijing_rank.getStr().equals("challenge")) {
            RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
            respGeneralNotify.setNotify(98);
            respGeneralNotify.setValue("SecretDoorWonderlandDlg");
            MessagePusher.pushMessage((Role)role, respGeneralNotify);
            ((MinjingHander)SpringUtils.getBean(MinjingHander.class)).doStartFight(role, (NPC)null);
         }

      }
   }

   public class MijingRaw implements RowMapper<MijingRank> {
      public MijingRank mapRow(ResultSet re, int arg1) throws SQLException {
         return new MijingRank(re.getInt("id"), re.getString("rolename"), re.getInt("guanqia"), re.getInt("wushang"), re.getInt("fashang"), re.getInt("fangyu"), re.getInt("sudu"), re.getString("gid"), re.getString("party"));
      }
   }
}
